﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TVGrid.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        public TVGridViewModel TvGridViewModel { get; set; }

        public NewStationViewModel NewStationViewModel { get; set; }

        public LoginViewModel LoginViewModel { get; set; }

        public MainViewModel()
        {
            LoginViewModel = new LoginViewModel();
            LoginViewModel.LoginAnnounce += new EventHandler<ViewModel.LoginViewModel.LoginEventArgs>(LoginViewModel_LoginAnnounce);
        }

        void LoginViewModel_LoginAnnounce(object sender, LoginViewModel.LoginEventArgs e)
        {
            //TvGridViewModel.bl
        }
    }


    public interface IMyMsg
    {
        string Id { get; set; }
    }

    public class LoginMsg : IMyMsg
    {
        public bool IsSuccess { get; set; }


        public string Id
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }

    public static class MyMessenger
    {
        private static readonly IList<Delegate> _subscribers = new List<Delegate>();

        public static void Subscribe<T>(Action<T> method) where T : IMyMsg
        {
            _subscribers.Add(method);
        }

        public static void Unsubscribe<T>(Action<T> method) where T : IMyMsg
        {
            _subscribers.Remove(method);
        }

        public static void Publish(IMyMsg msg)
        {
            foreach (var item in _subscribers)
            {
                var methodParam = item.Method.GetParameters().First();
                if (methodParam.ParameterType == msg.GetType())
                {
                    item.DynamicInvoke(msg);
                }                
            }
        }
    }
}
